package actors

import (
	"context"
	"fmt"

	"engine"
	"engine/db"
	"engine/stream/net/http"
	"gitee.com/ameise84/e3dds"
	"servers/app/node/api/common"
	"servers/app/node/api/misc"
	"servers/models/define"
	"servers/models/error_code"
	"servers/models/meta"
	"servers/models/redis_help"
)

func init() {
	// 用户注册
	_gService.RegisterProtocolHandlerWithName("/open-api/create", toolsCreate)
	engine.RegisterHttpGetRouter(_gService, "/backend-api/modify-player-ctrl", toolsCreate)
	// 用户登录
	engine.RegisterHttpGetRouter(_gService, "/open-api/get-game-url", toolsGetGameURL)
	// 获取余额
	// 游戏列表查询
	// 充值回收
	// 充值订单查询
	// 充值记录查询
	// 游戏记录查询
	// 游戏细单查询
	// 游戏注单统计查询
	// 踢出用户
	// 查询余额用户

	engine.RegisterHttpGetRouter(_gService, "/client-api/get-game-list", toolsGetGameList)
	engine.RegisterHttpGetRouter(_gService, "/client-api/get-game-log-list", toolsGetGameLogList)
	engine.RegisterHttpGetRouter(_gService, "/client-api/get-game-log-info", toolsGetGameLogInfo)
	engine.RegisterHttpGetRouter(_gService, "/client-api/get-game-log-info2", toolsGetGameLogInfo2)
}

func toolsCreate(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()
	common.OnCreateAccount(behavior, req, params)
	return true
}

func toolsGetGameURL(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()
	common.OnGetGameURL(behavior, req, params, _gDefaultAes)
	return true
}

func toolsGetGameList(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()

	checkFields := []string{
		define.WebToken,
		define.WebLang,
	}
	if !misc.VerifyHttpFields(checkFields, params) {
		common.ResponseFailed(req, error_code.ErrorCodeProtocolDataNotVerify)
		return true
	}

	cli := db.GetRedis()
	ctx := context.Background()
	urlParams := meta.GameURLParams{}
	if err := misc.UnparseGameUrlToken(_gDefaultAes, params[define.WebToken], &urlParams); err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeEncryptFailed)
		return true
	}
	r, err := cli.Get(ctx, fmt.Sprintf(redis_help.TokenToUserID, urlParams.Token)).Result()
	if err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeTokenExpired)
		return true
	}

	rs := redis_help.Split(r)
	args := make(map[string]string, 2)
	args[define.WebBusinessId] = rs[0]
	args[define.WebLang] = params[define.WebLang]
	common.OnGetGameList(behavior, req, args)
	return true
}

func toolsGetGameLogList(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()

	checkFields := []string{
		define.WebToken,
		define.WebGameKind,
		define.WebBeginTime,
		define.WebEndTime,
		define.WebPage,
		define.WebScoreType,
	}
	params[define.WebScoreType] = "1"
	if !misc.VerifyHttpFields(checkFields, params) {
		common.ResponseFailed(req, error_code.ErrorCodeProtocolDataNotVerify)
		return true
	}

	cli := db.GetRedis()
	ctx := context.Background()
	urlParams := meta.GameURLParams{}
	if err := misc.UnparseGameUrlToken(_gDefaultAes, params[define.WebToken], &urlParams); err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeEncryptFailed)
		return true
	}
	r, err := cli.Get(ctx, fmt.Sprintf(redis_help.TokenToUserID, urlParams.Token)).Result()
	if err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeTokenExpired)
		return true
	}

	rs := redis_help.Split(r)
	params[define.WebUserId] = rs[1]
	params[define.WebPageSize] = "20"
	common.OnGetGameLogList(behavior, req, params)
	return true
}

func toolsGetGameLogInfo(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()
	checkFields := []string{
		define.WebToken,
		define.WebLogID,
		define.WebTimeStamp,
		define.WebScoreType,
	}
	params[define.WebScoreType] = "1"
	if !misc.VerifyHttpFields(checkFields, params) {
		common.ResponseFailed(req, error_code.ErrorCodeProtocolDataNotVerify)
		return true
	}

	cli := db.GetRedis()
	ctx := context.Background()
	urlParams := meta.GameURLParams{}
	if err := misc.UnparseGameUrlToken(_gDefaultAes, params[define.WebToken], &urlParams); err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeEncryptFailed)
		return true
	}
	r, err := cli.Get(ctx, fmt.Sprintf(redis_help.TokenToUserID, urlParams.Token)).Result()
	if err != nil {
		common.ResponseFailed(req, error_code.ErrorCodeTokenExpired)
		return true
	}

	rs := redis_help.Split(r)
	params[define.WebUserId] = rs[1]
	common.OnGetGameLogInfo(behavior, req, params)
	return true
}

func toolsGetGameLogInfo2(behavior e3dds.Behavior, req e3dds.Request) bool {
	httpCtx := req.Context().(*http.Context)
	params := httpCtx.AllQuery()
	checkFields := []string{
		define.WebUserId,
		define.WebLogID,
		define.WebScoreType,
	}

	params[define.WebScoreType] = "1"
	if !misc.VerifyHttpFields(checkFields, params) {
		common.ResponseFailed(req, error_code.ErrorCodeProtocolDataNotVerify)
		return true
	}

	//cli := db.GetRedis()
	//ctx := context.Background()
	//urlParams := meta.GameURLParams{}
	//if err := misc.UnparseGameUrlToken(_gDefaultAes, params[define.WebToken], &urlParams); err != nil {
	//	common.ResponseFailed(req, error_code.ErrorCodeEncryptFailed)
	//	return true
	//}
	//r, err := cli.Get(ctx, fmt.Sprintf(redis_help.TokenToUserID, urlParams.Token)).Result()
	//if err != nil {
	//	common.ResponseFailed(req, error_code.ErrorCodeTokenExpired)
	//	return true
	//}

	// rs := redis_help.Split(r)
	// params[define.WebUserId] = rs[1]
	common.OnGetGameLogInfo(behavior, req, params)
	return true
}
